var app = getApp();
var api = require("../../utils/api.js");
var util= require("../../utils/libs.js");
var data = require("../../utils/date.js")
Page({
  data: {
    id:null,
    topic:'',
    title:'',
    description:'',
    user:'',
    list:[],
    detail:{},
    limit:999999,
    page:1,
    uid:"",
    step:"",
    stepNow:1,
    stepP:'',
    text_num:0,
    time:0,
    shareI:false,
    shareImage:''
  },
  onLoad (options) {
    let that = this
    that.setData({
      id:options.id
    })
    this.topicDetail()
    this.getShare()
    // 页面初始化 options为页面跳转所带来的参数
  },
  share(){
    this.setData({
      shareI:true
    })
  },
  closeImg() {
    this.setData({
      shareI:false
    })
  },
  saveI(event){
    var url = event.currentTarget.dataset.src
    console.log(url)
    wx.getSetting({
      success(res) {
        console.log(res)
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              wx.downloadFile({
                url: url,
                success(res){
                  console.log(res)
                  wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success(res) {
                      console.log(res)
                      wx.showToast({
                        title: '保存成功',
                        icon:'none',
                        duration:2000
                      })
                      that.setData({
                        shareI: false
                      })
                    },
                    fail(res) {
                      console.log(res)
                    }
                  })
                }
              })
            }
          })
        }else{
          wx.downloadFile({
            url: url,
            success(res) {
              console.log(res)
              wx.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,
                success(res) {
                  console.log(res)
                  wx.showToast({
                    title: '保存成功',
                    icon: 'none',
                    duration: 2000
                  })
                  that.setData({
                    shareI: false
                  })
                },
                fail(res) {
                  console.log(res)
                }
              })
            }
          })
        }
      },
      error(res){
        console.log(res)
      }
    })
  },
  getShare() {
    var that = this
    wx.request({
      // 获取token
      url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential',
      data: {
        appid: 'wxb78b3211945cae67',
        secret: 'ad9e02f1ea741473fa5615787c0e9d7e'
      },
      success(res) {
        wx.request({
          // 调用接口C
          url: 'https://api.weixin.qq.com/wxa/getwxacode?access_token=' + res.data.access_token,
          method: 'POST',
          data: {
            path: "pages/index/index",
            width: 430,
            auto_color: false,
            line_color: { r: 0, g: 0, b: 0 }
          },
          success(res) {
            let author = ''
            // res是二进制流，后台获取后，直接保存为图片，然后将图片返回给前台
            // 后台二进制怎么转图片？我也不会后台，学会了再贴代码
            if(that.data.user==null){
              author = '未知'
            }else{
               author = that.data.user.nickname
            }
            util.request(api.upload,{
              type:2,
              title:that.data.title,
              author:author,
              description:that.data.description
            },'POST').then(function(res){
              that.setData({
                shareImage: res.data.file
              })
            })
          }
        })
      }


    })
  },
  topicDetail(){
    let that = this
    let uid = wx.getStorageSync('uid')
    util.request(api.buildDetail,{
      id:that.data.id,
      uid:uid},
    "GET")
    .then(function (res) {
      if(res.error_code === 0){ 
        let count = res.data.build.step_time
        var arr = res.data.build
        arr.create_time = data.data(arr.create_time)
        if(arr.user==null){
           arr.user= {
             nickname:'匿名',
             avatar:'/../imgs/avatar.png'
           }
        }
        that.setData({
          detail:arr,
          description:arr.content.substring(0,30),
          title:arr.title,
          step:arr.step,
          id:arr.id,
          time:count,
          text_num:arr.text_number,
          user: arr.user
        })
        that.topicList()
      }
    })
  },
  topicList(){
    let that = this
    util.request(api.buildArticleList,{
      page:that.data.page,
      limit:that.data.limit,
      id:that.data.id
    },
    "GET")
    .then(function (res) {
      if(res.error_code === 0){
       var arr = res.data.list
        for(var i=0;i<arr.length;i++){
          if(arr[i].user==null){
            arr[i].user = { 
            nickname:'匿名'
            };
            
          }
          if(i>1){
              arr[i].nextUser = arr[i-1].user.nickname
            }
        }
        console.log(arr)
         that.setData({
          list:arr,
          stepP: arr.length,
          stepNow:Math.floor(arr.length/that.data.step)+1
         })
      }
    }) 
  },
  toWrting(){
   let that = this
   wx.navigateTo({
     url:"/pages/receive/receive?id="+that.data.id+"&&step="
     + that.data.step + "&&time=" + that.data.time + "&&text_num=" + that.data.detail.text_number
   })
  },
  goDetail(e){
    let id = e.currentTarget.dataset.id
    let that = this
    wx.navigateTo({
      url: "/pages/dragonarticle/dragonarticle?id=" + id + "&&step="+ that.data.step + "&&time=" + that.data.time + "&&text_num=" + that.data.text_num
    })
  }
})